/**
 *  @author yinghui.zhang  人员管理管理
 */
 
Ext.ns("cn.christ.web.action.dict.person");
var personForm = cn.christ.web.action.dict.person.PersonForm;
personForm  = function() {
	var  formPanel, win,  addOrgTree, comboxWithTree, impFormPanel, impWin, swfupload;

	return {
		
		createFormPanel : function() {
			formPanel =  new Ext.form.FormPanel( {
							id : 'formPanel',
							name : 'formPanel',
							labelAlign : 'right',
							labelWidth : 65,
							padding : "10",
							defaultType : 'textfield',
							defaults : { anchor : '90%' },
							items : [ {
								fieldLabel : '用户帐号',
								id:'username',
								name : 'user.loginName',
								maxLength:32,
								allowBlank : false
							},{
								fieldLabel : '用户帐号',
								id:'userId',
								name : 'user.id',
								hidden:true
							},{
								fieldLabel : '用户姓名',
								id:'name',
								maxLength:32,
								name : 'name',
								allowBlank : false
							},{
								fieldLabel : '用户密码',
								id:'password',
								inputType : 'password',
								name : 'user.password',
								maxLength:32,
								allowBlank : false
							}, {
								xtype : 'radiogroup',
								fieldLabel : "性别",
								id : "gender",
								name:'gender',
								defaults : {
									name : 'gender'
									},
							items : [{
										boxLabel : '男',
										inputValue : '1',
										checked : true
									}, {
										boxLabel : '女',
										inputValue : '2'
									}]
			
						}, {
								fieldLabel : '工号',
								id:'dutyNo',
								name : 'dutyNo',
								regex : /^([0-9])+[0-9]*$/,
								allowBlank : true,
								maxLength:16
							},{
								xtype:'displayfield',
								fieldLabel:'单位(机构)',
								id:'orgName',
								name:'org.name',
							},{
								id:'orgId',
								name:'org.id',
								hidden:true
							}, {
								id : 'companyId',
								name : 'companyId',
								hidden : true
							},
							{ 
								fieldLabel : '手机',
								id:'phone',
								name : 'phone',
								regex : /^(((13[0-9])|(15[^4,\D])|(18[0,5-9]))\d{8})$/,
								regexText:'格式错误',
								maxLength:11,
								allowBlank : true
							},{ 
								fieldLabel : 'EMail',
								id:'email',
								name : 'email',
								regex: /^\s*\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(\;\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*(\;)*\s*$/,
								regexText:'格式错误',
								maxLength:50,
								allowBlank : true
							},{
								fieldLabel : '职务',
								id:'duty',
								name : 'duty',
								maxLength:32,
								allowBlank : true
							}, {
							    xtype:'textarea',
								fieldLabel : '地址',
								id:'address',
								name : 'address',
								maxLength:100,
								allowBlank : true
							}, {
								id : 'orgIds',
								name : 'orgIds',
								hidden : true
							}, {
								id : 'windowmode',
								name : 'windowmode',
								hidden : true
							}, {
								id : 'id',
								name : 'id',
								hidden : true
							}]
				});
			 return formPanel;
		},
		
		createWindow : function() {
			win =  new ExtWin( {
						width : 420,
						height : 410,
						items : formPanel,
						buttons : [
								{
									text : '保存',
									iconCls : 'acceptIcon',
									handler : function() {
										var f = formPanel.getForm();
										if (f.isValid()){
											var mode = Ext.getCmp('windowmode').getValue();
											if (mode == 'add')
											{
											    personView.subForm('/person/add.action');
											}
											else
											{
												personView.subForm('/person/update.action');	
											}
										}
									}
								}, {
									text : '关闭',
									iconCls : 'deleteIcon',
									handler : function() {
										win.hide();
									}
								} ]
					});
			 return win;
		},
		
		createImpFormPanel: function(){
			impFormPanel =  new Ext.form.FormPanel({
				id : 'impFormPanel',
				name : 'impFormPanel',
				labelAlign : 'right',
				labelWidth : 65,
				padding : '10',
				defaults : {       //字段的公共属性
					anchor : '90%' //字段宽度占容器宽度的比例
				},
				items : [
					{
						xtype : "container",
						layout : "column",
						defaults : {
							border : false
						},
						items : [{
									width : 230,
									style : "padding-left:0px;",
									layout : "form",
									items : {
										xtype : "textfield",
										fieldLabel : "Excel文件",
										id : 'excelFileName',
										name : "excelFilePath",
										readOnly : true,
										anchor : "98%,98%"
									}
								}, {
									xtype : "button",
									text : "上传",
									iconCls : "uploadIcon",
									scope : personForm,
									handler : function() {
										this.createSwfUpload();
										swfupload.show();
									}
								}, {
									xtype : "button",
									text : "删除",
									style : "padding-left:5px;",
									iconCls : "remove",
									handler : function() {
										var idvl = Ext.getCmp('excelFileName').getValue();
										if(idvl && idvl!=''){
											var id_vl = idvl.split('|');
											Ext.getCmp('excelFileName').setValue('');
											Ext.Ajax.request({
												url : cfg.cxt + '/attach_delete.do',
												success : function(response) {
													var rt = Ext.decode(response.responseText);
													if (!rt.success){
														safetys.ajaxError(response);
													} 
												},
												failure : safetys.ajaxError,
												params : {
													"ids" : id_vl[0]
												}
											});
										}
									}
								}]
					}
				]
			});
			return impFormPanel;
		},
		
		createImpWin : function(){
			impWin =   new ExtWin(
					{
						width : 420,
						height : 120,
						items : impFormPanel,
						buttons : [{
							text : '确认',
							iconCls : 'acceptIcon',
							handler : function() {
								personView.subFileForm('/person_imp.do');
							}
						}, {
							text : '关闭',
							iconCls : 'deleteIcon',
							handler : function() {
								impWin.hide();
							}
						}]
					}
				);
			return impWin;
		},
		
		createSwfUpload : function(){
			if( !swfupload ) {
				swfupload = new SwfWin({
					uploadConfig : {
						fileSize : '10MB',
						border : false,
						singleFile : true,//是否上传队列中只能放置一个文件
						fileTypes : '*.xls;*.xlsx', // 在这里限制文件类型:'*.*'
						fileTypesDescription : 'Excel文件'
					},
					buttons : [
								{
									text : '确认',
									iconCls : 'acceptIcon',
									handler : function() {
									    	var ds  = UploadPanel.grid.getStore();
									    	var upnum =  ds.getCount();
									    	if( upnum == 0){
									    		Ext.Msg.alert('提示','上传列表中，不存在您要上传的文件！');
									    		return;
									    	}
									    	//因为是单文件上传，上传列表只存在一条记录 
									    	var r = ds.getAt(0);
									    	if (r.data.fileState == SWFUpload.FILE_STATUS.QUEUED
													|| r.data.fileState == SWFUpload.FILE_STATUS.IN_PROGRESS) {
									    		Ext.Msg.alert('提示','您所选的文件尚未上传！');
												return;
											}
									    	if(r.data.fileState == SWFUpload.FILE_STATUS.ERROR){
									    		Ext.Msg.alert('提示','文件未上传成功！');
												return;
									    	}
											var id = r.get('fileDbId');
											var path = r.get('fileServerPath');
											Ext.getCmp('excelFileName').setValue(id+'|'+path);
											swfupload.hide();
									}
								} ,{
									text : '取消',
									iconCls : 'deleteIcon',
									handler : function() {
										swfupload.hide();
									}
								} ]
				});
				
				swfupload.on('show',function(){
					 UploadPanel.grid.getStore().removeAll();
				});
				
			}
			return swfupload;
		} 
	}

}();
